package com.fira.api.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.fira.api.domain.dto.LoginDTO;
import com.fira.common.annotation.RepeatSubmit;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.core.domain.saas.entity.SysUser;
import com.fira.common.utils.StringUtils;
import com.fira.edu.domain.EduClass;
import com.fira.edu.domain.dto.*;
import com.fira.edu.domain.dto.parent.ClassScheduleListDTO;
import com.fira.edu.domain.vo.ClassScheduleListVO;
import com.fira.edu.service.IEduClassRollCallService;
import com.fira.edu.service.IEduClassService;
import com.fira.edu.service.IEduCourseScheduleService;
import com.fira.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 小程序-班级信息接口
 * @author tony
 * @date 2021-10-12
 */
@Api(value="班级信息接口",tags={"班级信息"})
@Controller
@RequestMapping("/api/class")
public class ClassController extends ApiController {
    @Autowired
    private IEduClassService eduClassService;
    @Autowired
    private IEduCourseScheduleService eduCourseScheduleService;
    @Autowired
    private IEduClassRollCallService eduClassRollCallService;
    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value="查询班级列表",tags={"查询班级列表"},notes="")
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult getClassList(HttpServletRequest request){
        LoginDTO loginDTO = getLogin(request);
        String tenantId = loginDTO.getCurrentTenantId();
//        String tenantId = "1448669384791859201";
        return AjaxResult.success("查询成功",eduClassService.selectClassInfoList(tenantId));
    }

    @ApiOperation(value="查询课表列表",tags={"查询课表列表"},notes="")
    @PostMapping("/courseScheduleList")
    @ResponseBody
    public AjaxResult getCourseScheduleList(@ApiParam(name="课表列表查询入参") @Valid ClassScheduleListDTO classScheduleListDTO){
        startPage();
        List<ClassScheduleListVO> voList = eduCourseScheduleService.selectCourseScheduleList(classScheduleListDTO);
        return AjaxResult.success("查询成功",getDataTable(voList));
    }

    @ApiOperation(value="新增班级",tags={"新增班级"},notes="")
    @PostMapping("/addClass")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addClass(@ApiParam(name = "新增班级入参") @Valid ClassDTO classDTO, HttpServletRequest request){
        //校验班级名称是否重复
        EduClass eduClass = new EduClass();
        eduClass.setClassName(classDTO.getClassName());
        if(CollectionUtils.isNotEmpty(eduClassService.selectEduClassList(eduClass))){
            return AjaxResult.error("班级姓名已存在");
        }
        LoginDTO loginDTO = getLogin(request);
        String tenantId = loginDTO.getCurrentTenantId();
        classDTO.setTenantId(tenantId);

        SysUser teacher = sysUserService.getById(classDTO.getTeacherId());
        classDTO.setTeacherName(teacher.getUserName());

        return AjaxResult.success("新增成功",eduClassService.insertEduClass(classDTO));
    }

    @ApiOperation(value="新增排课信息",tags={"新增排课信息"},notes="")
    @PostMapping("/addCourseSchedule")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSchedule(@ApiParam(name = "新增排课信息入参") @Valid CourseScheduleDTO courseScheduleDTO, HttpServletRequest request){
        LoginDTO loginDTO = getLogin(request);
        String tenantId = loginDTO.getCurrentTenantId();
        courseScheduleDTO.setTenantId(tenantId);
        List<CourseScheduleTimeDTO> list = JSONArray.parseArray(courseScheduleDTO.getCorseWeekListStr(), CourseScheduleTimeDTO.class);
        courseScheduleDTO.setCourseScheduleTimeDTOList(list);
        return AjaxResult.success("操作成功",eduCourseScheduleService.insertEduCourseSchedule(courseScheduleDTO));
    }

    @ApiOperation(value="查询班级课程点名信息",tags={"查询班级课程点名信息"},notes="")
    @PostMapping("/classRollCall")
    @ResponseBody
    public AjaxResult getClassRollCall(@ApiParam(name = "查询班级课程点名信息入参") QueryClassRollCallDTO queryClassRollCallDTO){
        return AjaxResult.success("查询成功",eduClassService.selectClassRollCall(queryClassRollCallDTO));
    }

    @ApiOperation(value="学生上课点名信息",tags={"学生上课点名信息"},notes="")
    @PostMapping("/studentRollCall")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addStudentRollCall(@ApiParam(name = "课程点名入参(集合JSON串)") @RequestParam String list){
        List<ClassRollCallDTO> classRollCallDTOList = JSONArray.parseArray(list,ClassRollCallDTO.class);
        return AjaxResult.success("点名成功",eduClassRollCallService.insertEduClassRollCall(classRollCallDTOList));
    }
}
